﻿from PySide6.QtWidgets import (
    QApplication, QMainWindow, QListWidget, QStackedWidget, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QListWidgetItem, QDialog, QFormLayout, QDialogButtonBox
)
from PySide6.QtCore import Qt

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Navigation Example")
        self.setGeometry(100, 100, 800, 600)

        # Main layout
        main_layout = QHBoxLayout()

        # Navigation bar
        self.nav_list = QListWidget()
        self.nav_list.addItem("根空间配置")
        self.nav_list.addItem("浏览本机空间")
        self.nav_list.addItem("浏览服务器空间")
        self.nav_list.addItem("创建空间")
        self.nav_list.currentRowChanged.connect(self.display_content)

        # Content area
        self.content_area = QStackedWidget()
        self.content_area.addWidget(self.create_root_config_widget())
        self.content_area.addWidget(self.create_local_space_widget())
        self.content_area.addWidget(QWidget())  # Placeholder for "浏览服务器空间"
        self.content_area.addWidget(QWidget())  # Placeholder for "创建空间"

        # Add widgets to main layout
        main_layout.addWidget(self.nav_list)
        main_layout.addWidget(self.content_area)

        # Set main layout
        container = QWidget()
        container.setLayout(main_layout)
        self.setCentralWidget(container)

    def create_root_config_widget(self):
        widget = QWidget()
        layout = QVBoxLayout()

        config_data = {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }

        for key, value in config_data.items():
            h_layout = QHBoxLayout()
            h_layout.addWidget(QLabel(f"{key}:"))
            line_edit = QLineEdit(value)
            line_edit.setReadOnly(True)
            h_layout.addWidget(line_edit)
            layout.addLayout(h_layout)

        widget.setLayout(layout)
        return widget

    def create_local_space_widget(self):
        widget = QWidget()
        layout = QVBoxLayout()

        self.local_space_list = QListWidget()
        self.local_space_list.addItem("Space 1: /path/to/space1")
        self.local_space_list.addItem("Space 2: /path/to/space2")
        self.local_space_list.itemClicked.connect(self.display_space_details)

        self.space_details = QWidget()
        self.space_details_layout = QVBoxLayout()
        self.space_details.setLayout(self.space_details_layout)

        self.edit_button = QPushButton("编辑")
        self.edit_button.clicked.connect(self.edit_space_details)
        self.save_button = QPushButton("保存")
        self.save_button.clicked.connect(self.save_space_details)
        self.copy_button = QPushButton("复制空间")
        self.copy_button.clicked.connect(self.copy_space)

        layout.addWidget(self.local_space_list)
        layout.addWidget(self.space_details)
        layout.addWidget(self.edit_button)
        layout.addWidget(self.save_button)
        layout.addWidget(self.copy_button)

        widget.setLayout(layout)
        return widget

    def display_content(self, index):
        self.content_area.setCurrentIndex(index)

    def display_space_details(self, item):
        dialog = QDialog(self)
        dialog.setWindowTitle("空间配置详情")
        layout = QVBoxLayout(dialog)

        details_data = {
            "key1": "value1",
            "key2": "value2",
            "key3": "value3"
        }

        for key, value in details_data.items():
            h_layout = QHBoxLayout()
            h_layout.addWidget(QLabel(f"{key}:"))
            line_edit = QLineEdit(value)
            line_edit.setReadOnly(True)
            h_layout.addWidget(line_edit)
            layout.addLayout(h_layout)

        edit_button = QPushButton("编辑")
        edit_button.clicked.connect(lambda: self.edit_space_details(layout))
        save_button = QPushButton("保存")
        save_button.clicked.connect(lambda: self.save_space_details(layout))

        button_layout = QHBoxLayout()
        button_layout.addWidget(edit_button)
        button_layout.addWidget(save_button)

        layout.addLayout(button_layout)
        dialog.setLayout(layout)
        dialog.exec()

    def edit_space_details(self, layout):
        for i in range(layout.count()):
            widget = layout.itemAt(i).widget()
            if isinstance(widget, QLineEdit):
                widget.setReadOnly(False)

    def save_space_details(self, layout):
        for i in range(layout.count()):
            widget = layout.itemAt(i).widget()
            if isinstance(widget, QLineEdit):
                widget.setReadOnly(True)

    def copy_space(self):
        dialog = QDialog(self)
        dialog.setWindowTitle("复制空间")
        layout = QFormLayout(dialog)

        name_edit = QLineEdit()
        path_edit = QLineEdit()
        layout.addRow("新空间名称:", name_edit)
        layout.addRow("新空间路径:", path_edit)

        buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
        buttons.accepted.connect(dialog.accept)
        buttons.rejected.connect(dialog.reject)
        layout.addWidget(buttons)

        if dialog.exec() == QDialog.Accepted:
            new_name = name_edit.text()
            new_path = path_edit.text()
            self.local_space_list.addItem(f"{new_name}: {new_path}")

if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()